// pages/addfriends/addfriends.js
const app = getApp()
var server = require('../../utils/server');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    care:[],
    user:[],
    PageIndex:1,
    search:'^',
    hasNext:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this
    that.user(1)
  },
  user: function (PageIndex) {
    server.requestForm(false, 'get', 'user', 
    {
      filter: {
        _id: app.globalData.userID
      }
    },
      function (e) {
        console.log(e)
        if(e.code == 0){
          that.setData({
            user: e.data[0]
          })
          that.care(PageIndex)
        }else{
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 1000,
          });
        }

    })
  },
  care: function (PageIndex){
    var care = that.data.care
    server.requestForm(false, 'get', 'user', {
      page: ({
        size: 10,
        index: PageIndex
      }),
    }, function (res) {

      if(res.code ==0){
        var careID = that.data.user.careID
        console.log(careID)
        if (careID){
          for (let i = 0; i < careID.length; i++) {
            for (let g = 0; g < res.data.items.length; g++) {
              if (careID[i] === res.data.items[g]._id) {
                res.data.items[g].gz = 1
              }
            }
          }
          for (let c = 0; c < res.data.items.length; c++) {
            care.push(res.data.items[c])
          }
        }

        that.setData({
          care: care,
          hasNext:res.data.hasNext
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 1000,
        });
      }

    })
  },
  //搜索
  ss: function (e) {
    console.log(e)
    var search = e.detail.value
    if (search == '') {
      that.setData({
        search: '^',
      })
    } else {
      that.setData({
        search: search,
      })
    }
  },
  sous: function (e) {
    that.setData({
      care: [],
      PageIndex:1
    })
    if (that.data.search=='^'){
      that.care(that.data.PageIndex)
    }else{
      that.care1(that.data.PageIndex, that.data.search)  
    }

  },
  care1: function (PageIndex, search) {
    server.requestForm(false, 'get', 'user/searchByNicknameOrContext', {
      page: {
        size: 10,
        index: PageIndex
      },
      filter: {
        search: search
      }
    }, function (res) {
      var careID = that.data.user.careID
      if (careID){
        for (let i = 0; i < careID.length; i++) {
          for (let g = 0; g < res.data.items.length; g++) {
            if (careID[i] === res.data.items[g]._id) {
              res.data.items[g].gz = 1
            }
          }
        }
      }

      that.setData({
        care: res.data.items
      })
    })
  },
  guanzu1: function (e) {
    console.log(e)
    var authorid = e.currentTarget.dataset.authorid
    var index = e.currentTarget.dataset.index
    var care = that.data.care
    if (care[index].gz == 1) {
      console.log('11')
      care[index].gz = 0;
      that.setData({
        care: care
      })
    } else {
      console.log('22')
      care[index].gz = 1;
      that.setData({
        care: care
      })
    }
    console.log('data', that.data.care)
    that.modifyCare(authorid)
  },
  modifyCare: function (authorID) {
    server.requestForm(false, 'put', 'user/modifyCare', {
      authorID: authorID,
      watcherID: app.globalData.userID
    },
      function (e) {
        console.log(e)
        wx.showToast({
          title: e.msg,
          icon: 'none',
          duration: 1000,
          mask: true
        });
      })
  },
  onReachBottom: function(e) {
    if (that.data.hasNext) {
      that.setData({
        PageIndex: that.data.PageIndex + 1
      })
      that.user(that.data.PageIndex)
      } else {

      }
    
  },
})